# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

from inter_game_spider.items import InfoItem, IgnoreLinkItem
from inter_game_spider.model.models import Info, IgnoreLink
from inter_game_spider.model.config import DBSession


class InterGameSpiderPipeline(object):
    def __init__(self):
        self.session = DBSession()

    def process_item(self, item, spider):
        if isinstance(item, InfoItem):
            self.deal_info(item)
        elif isinstance(item, IgnoreLinkItem):
            self.deal_ignore_link(item)
        else:
            pass
        return item

    def deal_info(self, item):
        """
        将数据存到数据库
        :param item:
        :return:
        """
        pub_time = item["pub_time"]
        title = item["title"]
        link = item["link"]
        content = item["content"]
        platform = item["platform"]
        author = item["author"]
        company = item["company"]
        news_type = item["news_type"]
        record_type = item["record_type"]
        create_date = item["create_date"]
        info = Info(pub_time=pub_time, title=title, link=link, content=content,
                    platform=platform, author=author, company=company, news_type=news_type,
                    record_type=record_type, create_date=create_date)
        if self.session.query(IgnoreLink.id).filter_by(link=link).count() > 0:
            print "link exists : %s"%link
        else:
            try:
                self.session.add(info)
                self.session.commit()
            except Exception as e:
                self.session.rollback()
                print e

    def deal_ignore_link(self, item):
        link = item["link"]
        create_date = item["create_date"]
        ignore_link = IgnoreLink(link=link, create_date=create_date)
        try:
            self.session.add(ignore_link)
            self.session.commit()
        except Exception as e:
            self.session.rollback()
            print e
